import store from '@/store'
import qs from 'qs'
import { getSign } from '@/utils/sign'

// download file
const downloadFile = (url, data) => {
  var requestData = Object.assign({}, data)
  requestData.timestamp = new Date().getTime()
  requestData.appkey = process.env.APP_KEY
  if (store.getters.token) {
    // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
    requestData.authToken = store.getters.token
  }
  requestData.sign = getSign(requestData)
  var requestUrl = process.env.BASE_API + url + '?' + qs.stringify(requestData)
  const iframe = document.createElement('iframe')
  iframe.style.display = 'none'
  iframe.src = requestUrl
  iframe.onload = function() {
    document.body.removeChild(iframe)
  }
  document.body.appendChild(iframe)
}

export default {
  downloadFile
}
